package in.muton.edushare.academics.panels;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.academics.grades.UnivLetterGrades;
import com.edushare.common.vo.Universities;

public class UnivGradeKeyPanel extends EdusharePanel {
	private static final long serialVersionUID = -4790719891744544269L;

	public static int UNIV_KEY_VIEW = 10;
	public static int UNIV_KEY_EDIT = 20;
	public static int UNIV_KEY_NONE = 30;

	private IModel<Universities> univModel;
	private int activePanel = UNIV_KEY_VIEW;

	public static final Log log = LogFactory.getLog(UnivGradeKeyPanel.class);

	public UnivGradeKeyPanel(String id, IModel<Universities> model,
			EduShareBasePage page) {
		super(id, page);
		add(CSSPackageResource.getHeaderContribution(UnivGradeKeyPanel.class,
				"GradeStyle.css"));
		this.univModel = model;
		UnivLetterGrades ulg = new UnivLetterGrades(getORMInterface());
		if (!ulg.hasLetterGrades(univModel.getObject()))
			activePanel = UNIV_KEY_NONE;

		WebMarkupContainer wmc = new WebMarkupContainer("noGradePanel") {
			@Override
			public boolean isVisible() {
				return activePanel == UNIV_KEY_NONE;
			}

			private static final long serialVersionUID = -7862418577825430966L;
		};
		add(wmc);
		wmc.add(new Form<Void>("f") {
			@Override
			public void onSubmit() {
				setActivePanel(UNIV_KEY_EDIT);
			}

			private static final long serialVersionUID = 8523613796348882372L;
		});

		WebMarkupContainer wmc2 = new WebMarkupContainer("gradePanel") {
			@Override
			public boolean isVisible() {
				return activePanel != UNIV_KEY_NONE;
			}

			private static final long serialVersionUID = -7862418577825430966L;
		};
		add(wmc2);

		wmc2.add(new UnivGradeKeyEdit("panel1", univModel, page, this));
		wmc2.add(new UnivGradeKeyView("panel2", univModel, page, this));

	}

	public void setActivePanel(int activePanel) {
		UnivLetterGrades ulg = new UnivLetterGrades(getORMInterface());
		if (activePanel == UNIV_KEY_VIEW
				&& !ulg.hasLetterGrades(univModel.getObject()))
			this.activePanel = UNIV_KEY_NONE;
		else
			this.activePanel = activePanel;
	}

	public int getActivePanel() {
		return activePanel;
	}
}
